<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
</head>
<body>
<input type="text" id="i1">
<input type="text" id="i2">
<input type="button" value="加" onclick="f(1)">
<input type="button" value="减" onclick="f(2)">
<input type="button" value="乘" onclick="f(3)">
<input type="button" value="除" onclick="f(4)">
<div>结果:<span></span></div>
<script>
    function f(x) {
        let i1 = document.querySelector("#i1");
        let i2 = document.querySelector("#i2");
        let s = document.querySelector("span");
        /* 判断输入的两个值是不是NaN */
        if (isNaN(i1.value) || isNaN(i2.value)) {
            s.innerText = "输入有误,请重新输入!";
            return;
        }
        switch (x) {
            case 1:
                s.innerText = parseFloat(i1.value) + parseFloat(i2.value);
                break;
            case 2:
                s.innerText = parseFloat(i1.value) - parseFloat(i2.value);
                break;
            case 3:
                s.innerText = parseFloat(i1.value) * parseFloat(i2.value);
                break;
            case 4:
                s.innerText = parseFloat(i1.value) / parseFloat(i2.value);
                break;
        }
    }
</script>
</body>
</html>